<template>
  <view>
    <view class="goods_list">
    <view v-for="(goods,i) in goodList" :key="i" @click="gotoDetail(goods)">
    <my-goods :goods="goods"></my-goods>
    </view>
      
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数对象
        queryObj:{
          //查询关键词
          query:'',
          //商品分类id
          cid:'',
          //页码值
          pagenum:1,
          //每页请求多少条数据
          pagesize:10
        },
        goodList:[],
        total:0,
        //节流阀
        isloading:false
       
      };
    },
    onLoad(options){
      this.queryObj.query=options.query || ''
      this.queryObj.cid=options.cid || ''
     // console.log(this.queryObj)
     this.getGoodList()
       
    
    },
    methods:{
     async getGoodList(cb){
       //打开节流阀
       this.isloading=true
      const{data:res} =await uni.$http.get('api/public/v1/goods/search',this.queryObj)
      //关闭节流阀
      this.isloading=false
      //只要数据存在，就立即按需要调用cb回调函数
      cb&&cb()  
      if(res.meta.status!==200){
        return uni.showToast({
          title:'数据加载失败',
          duration:1500,
          icon:'none'
        })
      }
      //解构赋值，将两个数组的值赋值给goodlist
      this.goodList=[...this.goodList,...res.message.goods]
      // this.goodList=this.goodList.concat(res.message.goods)
      this.total=res.message.total
      // console.log(this.goodList)
     
      },
      gotoDetail(goods){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
        
      }
    
    },
    onReachBottom(){
      //判断是否还有下一条数据
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.showToast({
             title:'数据加载完毕',
             duration:1500,
             icon:'none'
           })
      if(this.isloading) return 
      //下拉触底时让页码值加一
      this.queryObj.pagenum++
      this.getGoodList()
    },
    onPullDownRefresh(){
      //重置数据
      this.queryObj.pagenum=1
      this.total=0
      this.isloading=false
      this.goodList=[]
      
      //重新发送请求
      this.getGoodList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
